package com.journaldev.spring;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.journaldev.spring.model.City;
import com.journaldev.spring.model.District;
import com.journaldev.spring.service.CityService;
import com.journaldev.spring.service.DistrictService;
import com.journaldev.spring.util.SimpleCityEditor;

@Controller
public class DistrictController {
	
	private DistrictService districtService;
	
	@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(City.class, new SimpleCityEditor(cityService));
    }

	@Autowired(required=true)
	@Qualifier(value="districtService")
	public void setDistrictService(DistrictService districtService) {
		this.districtService = districtService;
	}
	
	private CityService cityService;
	
	@Autowired(required=true)
	@Qualifier(value="cityService")
	public void setCityService(CityService ps){
		this.cityService = ps;
	}
	
	@RequestMapping(value = {"/districts"}, method = RequestMethod.GET)
	public String listDistricts(Model model) {
		model.addAttribute("district", new District());
		model.addAttribute("listDistrict", this.districtService.listDistrict());
		model.addAttribute("listCities", this.cityService.listCity());
		return "district";
	}
	
	@RequestMapping(value = {"/district/add"}, method = RequestMethod.POST)
	public String addDistrict(@ModelAttribute("district") District p,  BindingResult result, Model model, RedirectAttributes redirectAttrs) {
		if(result.hasErrors()) {
			model.addAttribute("message", "add errors");
			return "redirect:/districts";
		}
		if(p.getId() != 0) {
			this.districtService.updateDistrict(p);
			redirectAttrs.addFlashAttribute("message", "update success id =" + p.getId());
		} else {
			this.districtService.addDistrict(p);
			redirectAttrs.addFlashAttribute("message", "add success");
		}
		model.asMap().clear();
		return "redirect:/districts";
	}
	
	@RequestMapping(value = {"/district/remove/{id}"})
	public String removeDistrict(@PathVariable("id") int id) {
		District temp = this.districtService.getDistrictById(id);
		City cityObj = this.cityService.getCityById(temp.getBelongToCity().getId());
		Iterator<District> ite = cityObj.getDisTrict().iterator();
		District element = null;
		while(ite.hasNext()) {
			 element = ite.next();
	         if(element.getId() == id) {
	        	 break;
	         }
        }
		cityObj.getDisTrict().remove(element);
		//this.cityService.getCityById(temp.getBelongToCity().getId()).setDisTrict(listDistrict);
		this.cityService.updateCity(cityObj);
		//temp.setBelongToCity(null);
		//this.districtService.updateDistrict(temp);
		//this.districtService.removeDistrict(id);
		return "redirect:/districts";
	}
	
	@RequestMapping("/district/edit/{id}")
    public String editPerson(@PathVariable("id") int id, Model model){
        model.addAttribute("district", this.districtService.getDistrictById(id));
        model.addAttribute("listCities", this.cityService.listCity());
        return "district";
    }

}
